package com.yurun.teacherorgmodule.service.Impl;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.Teacher;
import com.yurun.orgpermissionmodule.mapper.OrgPermissionMapper;
import com.yurun.teacherorgmodule.mapper.TeacherMapper;
import com.yurun.teacherorgmodule.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private OrgPermissionMapper permissionModuleMapper;


    @Override
    public CommonResult allTeacherInfo(String keyword, BigInteger organizationId, String branchId, String teamId) {
        if (keyword.equals("") && branchId.equals("0") && teamId.equals("0")) {
            return CommonResult.success(teacherMapper.getAllTeacherInfoByOrgId(organizationId));
        } else if (keyword.equals("") && branchId.equals("0") && !teamId.equals("0")) {
            return CommonResult.success(teacherMapper.getTeacherByOrgIdAndTeamId(organizationId, Integer.parseInt(teamId)));
        } else if (keyword.equals("") && !branchId.equals("0") && teamId.equals("0")) {
            return CommonResult.success(teacherMapper.getAllBranchTeacherInfoByBranchId(organizationId, BigInteger.valueOf(Long.parseLong(branchId))));
        } else if (keyword.equals("") && !branchId.equals("0") && !teamId.equals("0")) {
            return CommonResult.success(teacherMapper.getTeacherByBranchIdAndTeamId(organizationId, BigInteger.valueOf(Long.parseLong(branchId)), Integer.parseInt(teamId)));
        } else if (!keyword.equals("") && !branchId.equals("0") && teamId.equals("0")) { // 分店id+keyword
            return CommonResult.success(teacherMapper.getTeacherByBranchIdAndName(organizationId, BigInteger.valueOf(Long.parseLong(branchId)), keyword));
        } else if (!keyword.equals("") && branchId.equals("0") && teamId.equals("0")) { // 机构id+keyword
            return CommonResult.success(teacherMapper.getTeacherByOrgIdAndName(organizationId, keyword));
        }
        // 教研组
        return CommonResult.success(teacherMapper.getTeacherByTeamIdAndName(organizationId, keyword, Integer.parseInt(teamId)));
    }

    @Override
    public List<Teacher> getAllTeacherInfoByOrgId(BigInteger organizationId) {
        return teacherMapper.getAllTeacherInfoByOrgId(organizationId);
    }

    @Override
    public CommonResult editTeacher(Teacher teacher) {
        teacher.setUpdateTime(new Date());
        if (teacherMapper.editTeacher(teacher) == 2) {
            return CommonResult.success("修改成功！");
        }
        return CommonResult.failed("修改失败！");
    }

    @Override
    public List<Teacher> getAllBranchTeacherInfoByBranchId(BigInteger organizationId, BigInteger branchId) {
        return teacherMapper.getAllBranchTeacherInfoByBranchId(organizationId,branchId);
    }

    @Override
    public int delTeacherById(Teacher teacher) {
        BigInteger employeeId = teacher.getEmployeeId();
        BigInteger organizationId = teacher.getOrganizationId();
        BigInteger branchId = teacher.getBranchId();
        return teacherMapper.delTeacherById(employeeId,organizationId,branchId, new Date());
    }

    @Override
    public int addTeacher(BigInteger employeeId, BigInteger organizationId, BigInteger branchId, int teachingCategory) {
        Teacher teacher = new Teacher();
        teacher.setEmployeeId(employeeId);
        teacher.setOrganizationId(organizationId);
        teacher.setBranchId(branchId);
        teacher.setUpdateTime(new Date());
        teacher.setCreateTime(new Date());
        // 增加权限
        int[] arr_teacher = {10};
        int roleId = teacherMapper.getRoleList(employeeId).get(0);
        addPermission(roleId, arr_teacher);
        return teacherMapper.addTeacher(teacher);
    }

    @Transactional
    public void addPermission(int roleId, int[] arr) {
        // 增加权限
        for (int value : arr) {
            for (int list : teacherMapper.getAdminAllPermission(value)) {
                permissionModuleMapper.addPermissionByRoleId(roleId, list);
            }

        }
    }

    public void addTeacher(Teacher teacher, String flag) {
        if (flag.equals("2") || flag.equals("3") || flag.equals("4")) { // 教研组长
            // 增加权限
            int[] arr_teacher = {12};
            int roleId = teacherMapper.getRoleList(teacher.getEmployeeId()).get(0);
            teacher.setTeamId(Integer.parseInt(flag)); // 设置教研组
            addPermission(roleId, arr_teacher);
            teacherMapper.addTeacher(teacher);
        } else if (flag.equals("1")) {
            // 增加权限
            int[] arr_teacher = {10};
            int roleId = teacherMapper.getRoleList(teacher.getEmployeeId()).get(0);
            addPermission(roleId, arr_teacher);
            teacherMapper.addTeacher(teacher);
        }

    }
}
